﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SchoolLibrary.Entity;
using SchoolLibrary.DataBase.DAO;
using SchoolLibrary.Desktop.Guide.Input;

namespace SchoolLibrary.Desktop.Guide
{
    public class BookGuide : GuideForm<Book>
    {
        private string[] COLUMN_NAMES = { "id", "Навзание", "Автор", "Год", "Жанр", "Кол-во", "Цена" };

        public BookGuide() { }

        public BookGuide(ViewModeEnum mode) : base(mode) { } 

        protected override string[] getColumnNames()
        {
            return COLUMN_NAMES;
        }

        protected override object[] createRow(Book book)
        {
            List<object> row = new List<object>();
            row.Add(book.Id);
            row.Add(book.Name);
            row.Add(book.Authors);
            row.Add(book.Year);
            row.Add(book.GenreName);
            row.Add(book.Amount);
            row.Add(book.Price);           
            return row.ToArray();
        }

        protected override List<Book> findAll()
        {
            return BookDAO.getInstance().findAll();
        }


        protected override Book findById(long id)
        {
            return BookDAO.getInstance().findById(id);
        }


        protected override void addButton_Click(object sender, EventArgs e)
        {
            BookInputDialog dialog = new BookInputDialog();
            dialog.ShowDialog();
            Book book = dialog.Book;
            if (book != null)
            {
                BookDAO.getInstance().save(book);
            }
            this.initData();
        }

        protected override void editButton_Click(object sender, EventArgs e)
        {
            long id = getSelectedRowId();
            Book book = BookDAO.getInstance().findById(id);
            BookInputDialog dialog = new BookInputDialog(book);
            dialog.ShowDialog();
            BookDAO.getInstance().save(book);
            this.initData();
        }

        protected override void deleteButton_Click(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }
    }
}
